import time
class SimulatedTestbench():
    def __init__(self, test_pattern):
        self.step_index = 0
        self.test_pattern = test_pattern
        self.traffic_light = 0
        self.lidar_front = 0
        self.zebra_value = 0
        self.lidar_steering_value = 0
        self.lane_steering_value = 0
        self.speed_limit = None

    def getTrafficLight(self):
        return self.traffic_light
    def getLidarFront(self):
        return self.lidar_front
    def getZebraCrossing(self):
        return self.zebra_value
    def getLidarSteering(self):
        return self.lidar_steering_value
    def getLaneSteering(self):
        return self.lane_steering_value
    def getSpeedLimit(self):
        return self.speed_limit

    def step(self):
        current_pattern = self.test_pattern[self.step_index]
        assert len(current_pattern) == 2
        pattern = current_pattern[0]
        if type(pattern[0]) == int:
            self.traffic_light, self.lidar_front, self.zebra_value, self.lidar_steering_value,\
                    self.lane_steering_value, self.speed_limit = pattern
        elif type(pattern[0]) == str:
            eval(pattern[0])
        elif callable(pattern[0]):
            pattern[0]()

    def examine(self):
        current_pattern = self.test_pattern[self.step_index]
        examine_part = current_pattern[1]
        assert callable(examine_part[0])
        ret = False
        if examine_part[1] is None:
            ret = examine_part[0]()
        else:
            ret = examine_part[0](examine_part[1])
        assert ret, f"Fail to run {self.step_index}: {current_pattern}"
        self.step_index += 1
        return True if self.step_index < len(self.test_pattern) else False

